<!-- 物料/产品编码设置 -->
<template>
  <div>
    <CodeSetting
      :title="title"
      :breadCrumb="breadCrumb"
      :codeTitle="codeTitle"
      :list="numberList.list"
      @codeSettingSave="save"
    ></CodeSetting>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref, onMounted } from 'vue';
  import { useRouter } from 'vue-router';
  import { Message } from '@arco-design/web-vue';
  import {
    postOrderNumberSetting,
    getOrderNumberSetting,
  } from '@/api/order-management/client-management/order';
  import type {
    DemandNumber,
    DemandNumberItem,
  } from '@/api/order-management/client-management/demand/type';

  const router = useRouter();
  const title: string = '物料/产品编号设置';

  const breadCrumb: string[] = ['库存管理', '基础设置', '物料/产品编号设置'];

  const codeTitle: string = '物料/产品编号';

  const numberList = reactive<DemandNumber>({ list: [] });

  // 获取订单编号数据
  const getData = async () => {
    try {
      const res = await getOrderNumberSetting();
      res.forEach((item: DemandNumberItem) => {
        numberList.list.push(item);
      });
      numberList.list.sort((a: any, b: any) => a.sort - b.sort); // 按照返回sort的大小排序
    } catch (error: unknown) {
      console.log(error);
    }
  };
  onMounted(() => {
    getData();
  });

  const save = async (item: any) => {
    try {
      let flag = true;
      for (let i = 0; i < numberList.list.length; i += 1) {
        if (numberList.list[i].value === '' || numberList.list[i].name === '') {
          flag = false;
          Message.error('编号的名称或者内容不能为空!');
          break;
        }
        numberList.list[i].sort = i;
        numberList.list[i].type = 3; // 订单编号state默认为3
      }
      if (flag) {
        const data = {
          list: item,
        };
        await postOrderNumberSetting(data as any);
      }
    } catch (error: unknown) {
      console.log(error);
    }
  };
</script>

<style scoped lang="less"></style>
